package com.jw.springboot_jw.exception;

import com.jw.springboot_jw.pojo.ResponseMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常统一处理类
 * @author wuJiaWei
 * @version 1.0
 */
@RestControllerAdvice   // 全局异常处理 专门用于统一处理异常
public class GlobalExceptionHandlerAdvice {
    Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler({Exception.class})  //捕获所有异常
    public ResponseMessage<String> handleException(Exception e, HttpServletRequest request, HttpServletResponse response){
        //记录日志
        log.error("统一异常：",e);
        //返回异常信息
        return new ResponseMessage<>(500,"error",null);
    }
}
